#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"

void SeqListIntit(SeqList* ps) {
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}


void SeqListDestroy(SeqList* ps) {
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = ps->capacity = 0;
	}
}

void SeqListPrint(SeqList* ps) {
	assert(ps);
	size_t i = 0;
	for (i = 0; i < ps->size; i++) {
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void static check_capaticy(SeqList* ps) {
	assert(ps);
	if (ps->capacity == ps->size) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}
void SeqListPushBack(SeqList* ps, SLDateType x) {
	check_capaticy(ps);
	ps->a[ps->size] = x;
	ps->size++;

}
void SeqListPushFront(SeqList* ps, SLDateType x) {
	assert(ps);
	check_capaticy(ps);
	int end = ps->size-1;
	while (end >= 0) {
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
void SeqListPopFront(SeqList* ps) {
	if (ps->size > 0) {
		int begin = 0;
		while (begin < ps->size-1) {
			ps->a[begin] = ps->a[begin + 1];
			begin++;
		}
		ps->size--;
	}
}
void SeqListPopBack(SeqList* ps) {
	if (ps->size > 0) {
		ps->size--;
	}
}
int SeqListFind(SeqList* ps, SLDateType x) {
	int i = 0;
	for (i = 0; i < ps->size; i++) {
		if (ps->a[i] == x) {
			return i + 1;
		}
	}
	return -1;
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x) {
	assert(ps->a);
	assert(pos >=0&&pos<=ps->size);
	check_capaticy(ps);
	int end = ps->size ;
	while (pos < end) {
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;

}
void SeqListErase(SeqList* ps, int pos) {
	assert(ps->a);
	assert(pos >= 0 && pos < ps->size);
	int mod = pos;
	while (mod < ps->size-1) {
		ps->a[mod] = ps->a[mod + 1];
		mod++;
	}
	ps->size--;
}
